//
//  CFTabViewController.m
//  CFFinanceProject
//
//  Created by chunfeng on 2018/4/9.
//  Copyright © 2018年 xiaowen. All rights reserved.
//

#import "CFTabViewController.h"
#import "CFNavViewController.h"
#import "CFLoginViewController.h"
#import "CFHomeVC.h"
#import "CFLiveViewContriller.h"
#import "CFMineTableVC.h"
#import "CFMarketVC.h"

@interface CFTabViewController ()

@end

@implementation CFTabViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    [self setDelegate:self];
    
    [self setupChildControllers];
    
    [[UITabBar appearance] setBarTintColor:DEF_COLOR_TABBAR];
    //改变tabbar 线条颜色
    CGRect rect = CGRectMake(0, 0, DEF_SCREEN_WIDTH(), 1);
    UIGraphicsBeginImageContext(rect.size);
    CGContextRef context = UIGraphicsGetCurrentContext();
    CGContextSetFillColorWithColor(context,
                                   DEF_COLOR_LINE.CGColor);
    CGContextFillRect(context, rect);
    UIImage *img = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    
    [self.tabBar setShadowImage:img];
    
    [self.tabBar setBackgroundImage:[[UIImage alloc]init]];
    
    //这句表示取消tabBar的透明效果
    [UITabBar appearance].translucent = NO;
}

/**
 主控制器
 */
- (void)setupChildControllers{
    CFHomeVC * homeVC = (CFHomeVC *)[CFUtility getVC:@"CFHomeVC" Storyboard:HOME];
    homeVC.navigationItem.title = @"首页";
    [self addChildVCWith:homeVC title:@"首页" nmlImgName:@"tabbar_home" selImgName:@"tabbar_home_sel"];
    
    CFMarketVC * market = (CFMarketVC *)[CFUtility getVC:@"CFMarketVC" Storyboard:MAEKET];
    market.navigationItem.title = @"行情";
    [self addChildVCWith:market title:@"行情" nmlImgName:@"tabbar_stock" selImgName: @"tabbar_stock_sel"];
    
    UIViewController *threeVC = [[UIViewController alloc] init];
    threeVC.navigationItem.title = @"交易";
    [self addChildVCWith:threeVC title:@"交易" nmlImgName:@"tabbar_jiaoyi" selImgName: @"tabbar_jiaoyi_sel"];
    
    CFLiveViewContriller *liveVC = (CFLiveViewContriller *)[CFUtility getVC:@"CFLiveViewContriller" Storyboard:LIVE];
    liveVC.navigationItem.title = @"直播";
    [self addChildVCWith:liveVC title:@"直播" nmlImgName:@"tabbar_live" selImgName: @"tabbar_live_sel"];
    
    CFMineTableVC * mineVC = (CFMineTableVC *)[CFUtility getVC:@"CFMineTableVC" Storyboard:MINE];
    mineVC.navigationItem.title = @"我的";
    [self addChildVCWith:mineVC title:@"我的" nmlImgName:@"tabbar_mine" selImgName: @"tabbar_mine_sel"];
    
}

- (void)addChildVCWith:(UIViewController *)vc title:(NSString *)title nmlImgName:(NSString *)nmlImgName selImgName:(NSString *)selImgName {
    
    CFNavViewController *nav = [[CFNavViewController alloc] initWithRootViewController:vc];
    [self addChildViewController:nav];
    //设置标题
    nav.tabBarItem.title = title;
    
    [nav.tabBarItem setTitleTextAttributes:[NSDictionary dictionaryWithObjectsAndKeys:
                                            DEF_COLOR_GRAR, NSForegroundColorAttributeName,
                                            nil] forState:UIControlStateNormal];
    
    
    [nav.tabBarItem setTitleTextAttributes:[NSDictionary dictionaryWithObjectsAndKeys:
                                            DEF_COLOR_RED, NSForegroundColorAttributeName,
                                            nil] forState:UIControlStateSelected];
    
    //设置普通状态图片
    nav.tabBarItem.image = [[UIImage imageNamed:nmlImgName]
                            imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
    UIImage *selImg = [UIImage imageNamed:selImgName];
    selImg = [selImg imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
    nav.tabBarItem.selectedImage = selImg;
    
    nav.tabBarItem.imageInsets =UIEdgeInsetsMake(0,0, 0,0);
}

//- (void)tabBarController:(UITabBarController *)tabBarController didSelectViewController:(UIViewController *)viewController{    
//    
//    if (tabBarController.selectedIndex == 4) {
//        
//        [RACObserve(UserInfoShare, isLogin) subscribeNext:^(id x) {
//            
//            if ([x integerValue] == 0) {
//
//                CFLoginViewController * login = (CFLoginViewController *)[CFUtility getVC:@"CFLoginViewController" Storyboard:LOGIN];
//                UINavigationController *navc = [[UINavigationController alloc] initWithRootViewController:login];
//                [self presentViewController:navc animated:YES completion:^{
//                }];
//            }
//        }];
//    }
//}

-(BOOL)tabBarController:(UITabBarController *)tabBarController shouldSelectViewController:(UIViewController *)viewController{
//    if ([viewController.tabBarItem.title isEqualToString:@"我的"]){
//            if (!UserInfoShare.isLogin) {
//                CFLoginViewController * login = (CFLoginViewController *)[CFUtility getVC:@"CFLoginViewController" Storyboard:LOGIN];
//                UINavigationController *navc = [[UINavigationController alloc] initWithRootViewController:login];
//                [self presentViewController:navc animated:YES completion:^{
//                }];
//                return NO;
//            }
//    }
    return YES;
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
